package cn.itsource.controller;


import cn.itsource.basic.annotation.CustomeParam;
import cn.itsource.basic.aop.LogAop;
import cn.itsource.basic.aop.MyCache;
import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.domain.JSONResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.domain.org.Department;
import cn.itsource.service.DepartmentService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.sun.xml.internal.ws.api.model.MEP;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dept")
public class DepartmentController {


    @Autowired
    DepartmentService departmentService;


    @LogAop
    @PostMapping("/findOne")
    public PageList<Department> findOne(@CustomeParam Long id){
        //1.通过map接受单个的参数
        //Map<String,Long> map
        //Long id = map.get("id");
        //2.通过对象去接受
        //3.通过JSONObject 接受单个的参数
        //Object id = jsonObject.get("id");
        System.out.println(id);
        return null;
    }
    /**
     * 1.请求url:.dept/findPage
     * 2.请求方式;post
     * 3.请求参数 :{ pageNum : '',pageSize:'',keyword:''}
     * 4.PageList
     */
    @LogAop
    @PostMapping("/findPage")
    public PageList<Department> findPage(@RequestBody BaseQuery baseQuery){
        /**
         * 缓存步骤:
         * 1.先查询redis 是否有数据
         * 2.如果有直接返回结果,如果没有,查询数据库
         * 3.将数据从数据库查出存入redis
         * 4.返回结果到前端
         */
        //使用mybatisplus 完成分页的步骤
        //1,.创建page对象
        Page page = new Page<Department>();
        //当前页数
        //2.设置page对象的值
        page.setCurrent(baseQuery.getPage());
                //每页条数
        page.setSize(baseQuery.getPageSize());
        //3.完成分页查询
        Page selectPage = departmentService.selectPage(page);
        PageList pageList = new PageList<>(selectPage.getTotal(), selectPage.getRecords());
        return pageList;
    }

    /**
     *1.请求方式: get
     *2.url:dept/findTree
     *3.请求参数:null
     *4.响应参数: JSONResult
     **/
    @LogAop
    @GetMapping("/findTree")
    @ApiOperation(value="查询级联树形结构", notes="查询级联树形结构")
   // @Cacheable(cacheNames="deptTree",key="'tree1'")
    @MyCache(key = "deptTree:tree1")
    public List<Department> findTree(){

        return departmentService.findTree();
    }
}
